﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BluetoothMachine.Attributes;
using BluetoothMachine.Models;
using System.Collections.ObjectModel;
using System.Runtime.CompilerServices;
using System.ComponentModel;
using System.IO;
using System.Text.Json;
using System.Runtime.Serialization.Formatters.Binary;
using BluetoothMachine.ExpandHelper;
using BluetoothMachine.Domains;
using BluetoothMachine.Entity;
using NewLife.Serialization;

namespace BluetoothMachine.GlobalStatics
{
    /// <summary>
    /// 全局参数配置   单例
    /// </summary>
    [Singleton]
    public class ParameterConfigurationGlobal 
    {
        /// <summary>
        /// 全局参数
        /// </summary>
        public ParameterConfigurationModel ParameterModel { get; set; }

        public AsyncObservableCollection<FilePathModel> FilePath { get; set; }
        public ObservableCollection<RemoteControlCoordinatesModel> RCCItems { get; set; }
        public ObservableCollection<ProductionPointModel> PPItems { get; set; }
        public ObservableCollection<LabelModel> ContentList { get; set; }
        /// <summary>
        /// 红外模型List
        /// </summary>
        public List<InfraredModel> InfraredList { get; set; }

        public ParameterConfigurationGlobal()
        {
            ContentList = new ObservableCollection<LabelModel>();
            InfraredList = new List<InfraredModel>();
            RCCItems = new ObservableCollection<RemoteControlCoordinatesModel>();
            PPItems = new ObservableCollection<ProductionPointModel>();
            if (!File.Exists(FixedParameter.ParameterPath))
            {
                ParameterModel = new ParameterConfigurationModel(){ ProductModelList = new ObservableCollection<string>() };
            }
            else ParameterModel = JsonSerializer.Deserialize<ParameterConfigurationModel>(File.ReadAllText(FixedParameter.ParameterPath, Encoding.UTF8));
            if (!File.Exists(FixedParameter.FileModelPath))
            {
                FilePath = new AsyncObservableCollection<FilePathModel>();
                for (int i = 1; i < 11; i++)
                {
                    FilePath.Add(new FilePathModel()
                    {
                        Station = i
                    });
                }
            }
            else FilePath = JsonSerializer.Deserialize<AsyncObservableCollection<FilePathModel>>(File.ReadAllText(FixedParameter.FileModelPath, Encoding.UTF8));
            //if (!File.Exists(FixedParameter.PPPath)) PPSouce = new List<ProductionPointModel>();
            //else PPSouce = JsonSerializer.Deserialize<List<ProductionPointModel>>(File.ReadAllText(FixedParameter.PPPath, Encoding.UTF8));
            //if (!File.Exists(FixedParameter.RCCPath)) QPSouce = new List<QuickPointModel>();
            //else QPSouce = JsonSerializer.Deserialize<List<QuickPointModel>>(File.ReadAllText(FixedParameter.RCCPath, Encoding.UTF8));
            if (!File.Exists($"{FixedParameter.ConfigPath}\\productmodel.txt")) File.WriteAllText($"{FixedParameter.ConfigPath}\\productmodel.txt", "", Encoding.UTF8);
            ParameterModel.ProductModelList.Clear();
            ParameterModel.ProductModelList.AddRange(File.ReadAllLines($"{FixedParameter.ConfigPath}\\productmodel.txt", Encoding.UTF8));
            ParameterModel.Title = $"现在加工的产品为：{ParameterModel.ProductModel}";
            SelectRCCItems(-1, 0);
            SelectPPItems(-1, 0);
        }

        public void Save()
        {
            ParameterModel.Title = $"现在加工的产品为：{ParameterModel.ProductModel}";
            File.WriteAllText(FixedParameter.ParameterPath, JsonSerializer.Serialize(ParameterModel), Encoding.UTF8);
            File.WriteAllText(FixedParameter.FileModelPath, JsonSerializer.Serialize(FilePath), Encoding.UTF8);
        }

        //public void SaveRCC(int count)
        //{
        //    var model = QPSouce.Where(x => x.Title == ParameterModel.ProductModel && x.Index == count).FirstOrDefault();
        //    if (model != null) model.TPoint = RCCItems;
        //    else
        //    {
        //        QPSouce.Add(new QuickPointModel()
        //        {
        //            Title = ParameterModel.ProductModel,
        //            Index = count,
        //            TPoint = RCCItems,
        //        });
        //    }
        //    File.WriteAllText(FixedParameter.RCCPath, JsonSerializer.Serialize(QPSouce), Encoding.UTF8);
        //}

        //public void AddPPItem(ProductionPointModel model)
        //{
        //    PPItems.Add(model);
        //    PPSouce.Add(model);
        //}

        public void DelectPP(ProductionPointModel model)
        {
            if (model == null) return;
            try
            {
                var m = PPItems.FirstOrDefault(x => x.FuncType == model.FuncType && x.KeyName == model.KeyName);
                if(m != null) PPItems.Remove(m);
                ProductionPoint.Delete(
                    ProductionPoint._.ProductModel == model.ProductModel
                    & ProductionPoint._.FuncType == model.FuncType
                    & ProductionPoint._.Aisle == model.Aisle);
            }
            finally { }
        }

        public void SavePP(ProductionPointModel model, ProductionPoint entity, int count)
        {
            if (entity != null) {
                entity.KeyName = model.KeyName;
                entity.Index = model.Index;
                entity.IsEnable = model.IsEnable;
                entity.Ppx = model.Ppx;
                entity.Ppy = model.Ppy;
                entity.Ppz = model.Ppz;
                entity.ProductModel = model.ProductModel;
                entity.Aisle = model.Aisle;
                entity.FuncType = model.FuncType;
                entity.Save();
            }
            SelectPPItems(-1, count);
        }

        public void SelectPPItems(int up, int value)
        {
            if (up == value) return;
            if (value == -1) { PPItems.Clear(); return; }
            PPItems.Clear();
            var list = ProductionPoint.FindAll(ProductionPoint._.Aisle == value & ProductionPoint._.ProductModel == ParameterModel.ProductModel).ToJson().ToJsonEntity<List<ProductionPointModel>>();
            if (list != null && list.Count > 0)
            {
                list.Sort((x, y) => x.CompareTo(y));
                PPItems.AddRange(list);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="up">上一个索引</param>
        /// <param name="value">新索引</param>
        public void SelectRCCItems(int up,int value)
        {
            if (up == value) return;
            if (value == -1) { RCCItems.Clear(); return; }
            RCCItems.Clear();
            var list = RemoteControlCoordinates.FindAll(RemoteControlCoordinates._.ProductModel == ParameterModel.ProductModel);
            if (list != null)
            {
                foreach (var item in list)
                {
                    RCCItems.Add(new RemoteControlCoordinatesModel()
                    {
                        KeyName = item.KeyName,
                        Rccz = item.Rccz,
                        Rccy = item.Rccy,
                        Rccx = item.Rccx,
                        ProductModel = item.ProductModel
                    });
                }
            }
        }
    }
}
